﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Charts_GPS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //You must specify Google Map API Key for this component. You can obtain this key from http://code.google.com/apis/maps/signup.html
        //For samples to run properly, set GoogleAPIKey in Web.Config file.
        GoogleMapForASPNet1.GoogleMapObject.APIKey = ConfigurationManager.AppSettings["GoogleAPIKey"];

        //Specify width and height for map. You can specify either in pixels or in percentage relative to it's container.
        GoogleMapForASPNet1.GoogleMapObject.Width = "800px"; // You can also specify percentage(e.g. 80%) here
        GoogleMapForASPNet1.GoogleMapObject.Height = "600px";

        //Specify initial Zoom level.
        GoogleMapForASPNet1.GoogleMapObject.ZoomLevel = 12;

        //Specify Center Point for map. Map will be centered on this point.
        GoogleMapForASPNet1.GoogleMapObject.CenterPoint = new GooglePoint("1", 34.089061, 49.687042);

        if (IsPostBack)
        {
            ChartLinkButton_Click(ChartLinkButton, e);
        }
    }

    protected void ChartLinkButton_Click(object sender, EventArgs e)
    {
        List<tbCenter> _tbCenter = new List<tbCenter>();
        ICTBillingEntities _ICTBillingEntities = new ICTBillingEntities();
        _tbCenter = _ICTBillingEntities.tbCenters.Where(item => item.PkCenterID > 0).ToList();

        for (int i = 0; i < _tbCenter.Count; i++)
        {
            if (_tbCenter[i].LA != null && _tbCenter[i].LE != null)
            {
                GooglePoint GP1 = new GooglePoint();
                GP1.ID = "GP" + i.ToString();
                GP1.Latitude = (double)_tbCenter[i].LA;
                GP1.Longitude = (double)_tbCenter[i].LE;

                //Specify bubble text here. You can use standard HTML tags here.
                tbBill _tbBill = new tbBill();
                int id = _tbCenter[i].PkCenterID;
                byte Type = byte.Parse(TypeDropDownList.SelectedValue);

                if ((_ICTBillingEntities.tbBills.Where(item => item.FkCenterID == id && item.Type == Type).Count()) > 0)
                {
                    string lableType = "";
                    if (TypeDropDownList.SelectedValue == "2")
                        lableType = "کیلو وات";
                    else
                        lableType = "متر مکعب";

                    _tbBill = _ICTBillingEntities.tbBills.Where(item => item.FkCenterID == id && item.Type == Type).First();
                    GP1.InfoHTML = "نام مرکز: " + _tbCenter[i].CenterName + '\n' + "مقدار مصرف: " + _tbBill.UsedValue.ToString() + " " + lableType;

                    //Specify icon image. This should be relative to root folder.
                    List<spCenterStandardUsage_Result> sp = _ICTBillingEntities.spCenterStandardUsage(id, DateTime.Now, Type).ToList();
                    float usage = float.Parse(sp[0].UsedValue.ToString());
                    float min = float.Parse(sp[0].MinUsage.ToString());
                    float max = float.Parse(sp[0].MaxUsage.ToString());

                    if (usage < min)
                        GP1.IconImage = "icons/green-pin.png";
                    else if (usage > max)
                        GP1.IconImage = "icons/orange-pin.png";
                    else
                        GP1.IconImage = "icons/red-pin.png";
                }
                else
                {
                    GP1.InfoHTML = "نام مرکز: " + _tbCenter[i].CenterName;
                    GP1.IconImage = "icons/blue-pin.png";
                }

                GoogleMapForASPNet1.GoogleMapObject.Points.Add(GP1);
            }
        }
    }
}